package com.moji.controller.web;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.moji.common.base.ApiResult;
import com.moji.common.entity.MyPage;
import com.moji.entity.*;
import com.moji.entity.vo.CommonHomePageDTO;
import com.moji.entity.vo.CommonIndexDTO;
import com.moji.service.MjBannerService;
import com.moji.service.MjHomePageService;
import com.moji.service.MjProductService;
import com.moji.service.MjSolutionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 首页相关 前端控制器
 * </p>
 *
 * @author myg
 * @since 2024-07-23
 */
@RestController
@RequestMapping("/web/homepage")
@Api(tags = "首页相关web接口", value = "首页相关web接口")
public class MjHomepageController {
    /**
     * 服务对象
     */
    @Resource
    private MjHomePageService mjHomePageService;
    @Resource
    private MjProductService mjProductService;
    @Resource
    private MjSolutionService mjSolutionService;

    /**
     * 查询首页数据数据
     *
     * @param
     * @return
     */
    @PostMapping("/queryByPage")
    @ApiOperation(value = "查询首页相关数据，type非必传")
    public ApiResult<List<MjHomepage>> queryByPage(@RequestBody CommonHomePageDTO commonHomePageDTO) {
        List<MjHomepage> mjBannerMyPage = mjHomePageService.queryByPage(commonHomePageDTO);
        for (int i = 0; i < mjBannerMyPage.size(); i++) {
            if (mjBannerMyPage.get(i).getType() == 1) {
                LambdaQueryWrapper<MjSolution> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MjSolution::getId, mjBannerMyPage.get(i).getTargetUrl());
                List<MjSolution> list = mjSolutionService.list(queryWrapper);
                if (list != null&&list.size()>0) {
                    mjBannerMyPage.get(i).setRouter(list.get(0).getRouter());
                }
            } else if (mjBannerMyPage.get(i).getType() == 3) {
                LambdaQueryWrapper<MjProduct> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(MjProduct::getId, mjBannerMyPage.get(i).getTargetUrl());
                List<MjProduct> list = mjProductService.list(queryWrapper);
                if (list != null) {
                    mjBannerMyPage.get(i).setRouter(list.get(0).getRouter());
                }
            }
        }
        return ApiResult.ok(mjBannerMyPage);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @PostMapping("/detail")
    @ApiOperation(value = "根据id查看基础信息详情", response = MjHomepage.class)
    public ApiResult<MjHomepage> queryById(@RequestParam("id") Long id) {
        return ApiResult.ok(this.mjHomePageService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param mjBasicInfo 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation(value = "增加网站基本信息", response = MjHomepage.class)
    public ApiResult<MjHomepage> add(@Validated @RequestBody MjHomepage mjBasicInfo) {
        return ApiResult.ok(this.mjHomePageService.insert(mjBasicInfo));
    }

    /**
     * 编辑数据
     *
     * @param mjBasicInfo 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    @ApiOperation(value = "修改网站基本信息", response = MjHomepage.class)
    public ApiResult<MjHomepage> edit(@Validated @RequestBody MjHomepage mjBasicInfo) {
        return ApiResult.ok(this.mjHomePageService.update(mjBasicInfo));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除网站基本信息", response = Boolean.class)
    public ApiResult<Boolean> deleteById(@RequestParam("id") Long id) {
        return ApiResult.ok(this.mjHomePageService.deleteById(id));
    }

}

